package com.imooc.zuul.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : 浦希成
 * @date : 2018/8/2 17:12
 */
public class CookieUtil {
    /**
     * 设置cookie
     * @param response  HttpServletResponse对象
     * @param name key
     * @param value value
     * @param maxAge 过期时间
     */
    public static void set(HttpServletResponse response,String name,String value,int maxAge){
        Cookie cookie=new Cookie(name,value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

    public static Cookie get(HttpServletRequest request,String name){
        Cookie[] cookies=request.getCookies();
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                if(name.equals(cookie.getName())){
                    return cookie;
                }
            }
        }
        return null;
    }
}
